]> git.r.bdr.sh - rbdr/super-polarity/blobdiff - Super Polarity/ActorManager.cs
Protoshow sprint.
[rbdr/super-polarity] / Super Polarity / ActorManager.cs
index 1b4ef96c6e1c506a690a0b6c9899e782879d0287..f5587b9c9f772872a3915720451706b6fe3efcd8 100644 (file)
@@ -10,9 +10,9 @@ namespace SuperPolarity
     static class ActorManager
     {
 
-        static Game Game;
+        static SuperPolarity Game;
         static int OutlierBounds;
-        static List<Actor> Actors;
+        static IList<Actor> Actors;
 
         static ActorManager()
         {
@@ -27,7 +27,9 @@ namespace SuperPolarity
 
         static public void CheckOut(Actor actor)
         {
+            actor.CleanUp();
             Actors.Remove(actor);
+            actor = null;
         }
 
         static public void Update(GameTime gameTime) 
@@ -50,12 +52,22 @@ namespace SuperPolarity
 
         static void CheckActors()
         {
-            var i = 0;
-            foreach (Actor actor in Actors)
+            for (var i = Actors.Count - 1; i >= 0; i--)
             {
-                i++;
-                foreach (Actor other in Actors.Skip(i))
+                if (i >= Actors.Count) {
+                    i = Actors.Count - 1;
+                }
+
+                if (Actors.Count == 0)
                 {
+                    return;
+                }
+
+                Actor actor = Actors[i];
+                for (var j = i - 1; j >= 0; j--)
+                {
+                    Actor other = Actors[j];
+
                     CheckCollision(actor, other);
 
                     if (actor.GetType().IsSubclassOf(typeof(Ship)) && other.GetType().IsSubclassOf(typeof(Ship)))
@@ -68,6 +80,13 @@ namespace SuperPolarity
 
         static void CheckCollision(Actor actor, Actor other)
         {
+            var collision = Rectangle.Intersect(actor.Box, other.Box);
+            var inverseCollision = Rectangle.Intersect(other.Box, actor.Box);
+            if (!collision.IsEmpty && !actor.Dying && !other.Dying)
+            {
+                actor.Collide(other, collision);
+                other.Collide(actor, inverseCollision);
+            }
 
         }
 
@@ -99,13 +118,43 @@ namespace SuperPolarity
                     actor.Position.Y > Game.GraphicsDevice.Viewport.Height + OutlierBounds)
                 {
                     CheckOut(actor);
+                    if (actor.Parent != null)
+                    {
+                        actor.Parent.Children.Remove(actor);
+                    }
                 }
             }
         }
 
-        internal static void SetGame(Game game)
+        static public void Empty()
+        {
+            foreach (Actor actor in Actors) {
+                actor.CleanUp();
+            }
+            Actors.Clear();
+        }
+
+        internal static void SetGame(SuperPolarity game)
         {
             Game = game;
         }
+
+        public static void Bomb()
+        {
+            for (var i = Actors.Count - 1; i >= 0; i--)
+            {
+                var actor = Actors[i];
+                if (actor.GetType() == typeof(StandardShip))
+                {
+                    CheckOut(actor);
+                    Renderer.CheckOut(actor);
+                }
+            }
+        }
+
+        public static int CountBaddies()
+        {
+            return Actors.Where(a => a.GetType() == typeof(StandardShip)).Count();
+        }
     }
 }